@model EditPartViewModel
@using OrchardCore.ContentManagement.Metadata.Models
@using OrchardCore.ContentManagement.Metadata.Settings
@using OrchardCore.Mvc.Utilities

@{
    var orderedFields = Model.PartDefinition.Fields
        .Select(field => new { Field = field, Order = int.Parse(field.GetSettings<ContentPartFieldSettings>().Position ?? "0") })
        .OrderBy(x => x.Order)
        .Select(x => x.Field);
}
<h1>@RenderTitleSegments(T["Edit Content Part - {0}", Model.DisplayName])</h1>

<form asp-action="EditPart">
    @Html.ValidationSummary()

    <div class="form-group">
        <div class="row col-sm">
            <label asp-for="Name">@T["Technical Name"]</label>
            <input asp-for="Name" class="form-control" readonly />
            <span class="hint">@T["Technical name of the part."]</span>
        </div>
    </div>

    @await DisplayAsync(Model.Editor)

    <div class="form-group">
        <h3>@T["Fields"]</h3>

        @if (Model.PartDefinition != null)
        {
            <ul class="list-group sortable" id="fields" style="cursor: move;">
                @foreach (var field in orderedFields)
                {
                    <li class="list-group-item">
                        <div class="properties">
                            <div class="related">
                                <a asp-route-action="EditField" asp-route-id="@Model.PartDefinition.Name" asp-route-name="@field.Name" asp-route-returnUrl="@FullRequestPath" class="btn btn-primary btn-sm" role="button">@T["Edit"]</a>
                                <a asp-route-action="RemoveFieldFrom" asp-route-id="@Model.PartDefinition.Name" asp-route-name="@field.Name" class="btn btn-danger btn-sm" role="button" itemprop="UnsafeUrl RemoveUrl">@T["Remove"]</a>
                            </div>
                            @field.DisplayName() <span class="hint">— @field.FieldDefinition.Name.CamelFriendly()</span>
                        </div>
                        <input type="hidden" name="OrderedFieldNames" value="@field.Name" />
                    </li>
                }
            </ul>
        }
    </div>
    <div class="form-group">
        <a asp-route-action="AddFieldTo" asp-route-id="@Model.PartDefinition.Name" asp-route-returnUrl="@FullRequestPath" class="btn btn-info btn-sm" role="button">@T["Add Field"]</a>
    </div>

    <div class="form-group">
        <button class="btn btn-primary" type="submit" name="submit.Save" value="Save">@T["Save"]</button>
        <button class="btn btn-danger" type="submit" name="submit.Delete" value="Delete" itemprop="RemoveUrl">@T["Delete"]</button>
    </div>
</form>

<script at="Foot" type="text/javascript">
    //<![CDATA[
    $(function () {
        Sortable.create(document.getElementById("fields"));
    })
    //]]>
</script>
